package com.neusoft.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neusoft.entity.Demand;
import com.neusoft.entity.common.CommonResult;
import com.neusoft.service.DemandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

@RestController
@RequestMapping("demand")
public class DemandController {
    /**
     * 服务对象
     */
    @Resource
    @Autowired
    private DemandService demandService;

    @GetMapping("queryById/{id}")
    @ResponseBody
    public Object queryById(@PathVariable Integer id){
        System.out.println("查询单条数据"+id);
        Demand bean = demandService.queryById(id);
        System.out.println("bean对象为："+bean);
        return bean;
    }
    @PostMapping("deleteById/{ids}")
    @ResponseBody
    public boolean deleteById(@PathVariable Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return false;
        }
        System.out.println(ids);
        demandService.deleteById(Arrays.asList(ids));
        return true;
    }

    @PostMapping("delete1/{id}")
    @ResponseBody
    public boolean deleteById1(@PathVariable Integer id) {
        System.out.println("删除单条学生数据");
        if ( id == null) {
            return false;
        }
        System.out.println(id);
        return demandService.deleteById1(id);
    }

    @RequestMapping("queryAll/{page}/{limit}")
    @ResponseBody
    public Object queryAll(@PathVariable Integer page, @PathVariable Integer limit,@RequestBody Demand bean) {
        System.out.println(bean+"jj");
        CommonResult<Demand> result = new CommonResult<>();
        System.out.println(bean+"kk");
        IPage<Demand> ipage = demandService.queryAllByLimit(page,limit,bean);
//        System.out.println(bean+"ll");
        result.setCode(0);
        result.setCount(ipage.getTotal());
        result.setData(ipage.getRecords());
        System.out.println("学生数据查询成功");
        System.out.println(result);
        return result;
    }

    @RequestMapping("save")
    @ResponseBody
    public Object save(@RequestBody Demand bean) {
        System.out.println("修改和添加学生数据");
        boolean result = false;
        if (bean.getId() != null) {
            System.out.println("修改");
            result = demandService.update(bean) > 0;
        } else {
            result = demandService.insert(bean).getId() != null;
        }
        return result;
    }

}
